# -*- coding: utf-8
#随时间变化的延迟
import numpy as np
import seaborn as sns
import sys
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
import pandas as pd
import scipy
import os
import sys
import matplotlib.pyplot as plt
from pylab import * 
import math
from numpy import cumsum


#数据输入
def get_trace_reference(filename):
    
    file_base = open(filename)
    
    flag=0
    
    x = []
    y = []
    
    while 1:
        
        base_lines = file_base.readlines(100000)
             
        if (not base_lines):
            
            break 
            
        for base_line in base_lines:

            if str(base_line).startswith("      arrive"):

                flag=1
                
                continue

            if str(base_line).startswith("the 0 channel,"):

                flag=0
                
            if(flag==1):
                
                if(len(base_line.split())!=7):
                    
                    print("error\n")
                    
                    print(base_line.split(),len(base_line.split()))
                    
                if(int(base_line.split()[3])==1):
                    if(int(base_line.split()[6])!=1000):
                        x.append(int(base_line.split()[4])/1e9/3600)
                        y.append(int(base_line.split()[6])/1e6)
    file_base.close()
    x_1 = []
    y_1 = []
    for i in range(len(x)):
        if(x[i]>40 and x[i]<55):
            x_1.append(x[i])
            y_1.append(y[i])
    
    print(len(x_1))
    print(len(y_1))


    return x_1,y_1
    # return x,y
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
tick_params(direction='in')
ax = plt.gca()
    
#参数：对比输出文件 本身输出文件 输出文件夹+图片前缀
x1,y1 = get_trace_reference(sys.argv[1])
x2,y2 = get_trace_reference(sys.argv[2])
plt.plot(x1,y1,'b--',label="BaseLine")
plt.plot(x2,y2,'r--',label="Copyback")
 
plt.ylabel(u"读请求延迟（单位：毫秒）")
plt.xlabel(u"时间（单位：小时）")
plt.legend(loc = 'best') 
plt.tight_layout()
plt.savefig(sys.argv[3]+"op_read.png", bbox_inches = 'tight')
plt.show() 